"""
数据库模型基类
包含通用字段和方法
"""

from tortoise.models import Model
from tortoise import fields
from datetime import datetime


class BaseModel(Model):
    """
    基础模型类，包含通用字段
    """
    id = fields.IntField(pk=True, description="主键ID")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
    is_deleted = fields.BooleanField(default=False, description="是否删除")
    
    class Meta:
        abstract = True  # 标记为抽象模型，不会创建对应的数据表
        
    def soft_delete(self):
        """软删除方法"""
        self.is_deleted = True
        return self.save()
    
    @classmethod
    async def get_active_objects(cls):
        """获取未删除的对象"""
        return cls.filter(is_deleted=False)
        
    def to_dict(self):
        """转换为字典格式"""
        return {
            field: getattr(self, field) 
            for field in self._meta.fields_map.keys()
            if hasattr(self, field)
        }